package cn.cjp.day02;

import java.nio.charset.StandardCharsets;

public class UseString {
    public static void main(String[] args) {

        //空参构造
        String str1 = new String();
        System.out.println(str1);

        //字符数组创建
        char[] carray = {'a','b','c'};
        String str2 = new String(carray);
        System.out.println(str2);

        //字节数组创建
        byte[] barray = {97,98,99};
        String str3 = new String(barray);
        System.out.println(str3);

        //直接创建
        String str4 = "abc";
        System.out.println(str4);

        //直接创建在字符串常量池   池中的字符串是共享
        String str5 = "abc";
        System.out.println(str4 == str5);
        System.out.println(str1 == str4);
        System.out.println("===========================");

        //内容比较
        System.out.println(str3.equals(str2));
        //吧abc放在前更好，防止空指针异常
        System.out.println("Abc".equals(str3));
        //大小写
        System.out.println("ABc".equalsIgnoreCase(str2));
        System.out.println("===========================");

        //长度
        int length = "dsafhdsjkjfds".length();
        System.out.println("长度"+length);

        //拼接
        String str6 = "good ";
        String str7 = "mongning ";
        System.out.println(str6.concat(str7));

        //获取字符
        char a = "AaBb".charAt(2);
        System.out.println(a);

        //查找   返回第一个的索引
        int finden = "aaaabbbbb".indexOf("aa");
        System.out.println(finden);
        System.out.println(str2.indexOf("sdf"));
        System.out.println("===========================");

        //从参数位置截取到末尾
        String str8 = "abcdefghijk";
        String str9 = str8.substring(2);
        System.out.println(str9);
        //指定位置截取
        String str10 = str8.substring(2,4);
        System.out.println(str10);
        System.out.println("===========================");

        //转换字符串
        char[] char1 = "Hello".toCharArray();
        byte[] char2 = "Hello".getBytes();
        for (int i = 0; i < char1.length; i++) {
            System.out.println(char1[i]);
        }
        for (int i = 0; i < char2.length; i++) {
            System.out.println(char2[i]);
        }

        //替换
        String str11 = str8.replace("abc","ABC");
        System.out.println(str11);
        System.out.println("===========================");

        //分割   注意split参数是正则表达时，要用英文句点分割要用"\\."
        String str12 = "aa,bb,cc";
        String[] arraystr =str12.split(",");
        for (int i = 0; i < arraystr.length; i++) {
            System.out.println(arraystr[i]);
        }

    }
}
